﻿
//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

#include "CmdGRepProbe.h"
#include "IUiView.h"
#include "IElement.h"
#include "IDocument.h"
#include "ElementId.h"
#include "ISelection.h"
#include "IModelView.h"
#include "IUiDocument.h"
#include "GcmpCommandState.h"
#include "CommandParameters.h"
#include "IUiDocumentViewManager.h"

#include "GRepProbe.h"
#include "GRepCommandIds.h"
#include "UiCommonDialog.h"
#include "CommandRegister.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

using namespace gcmp;
using namespace Sample;

REGISTER_COMMAND(CmdViewGRepProbe)

Sample::CmdViewGRepProbe::CmdViewGRepProbe()
    : CommandBase(ID_CMD_VIEW_GREP_PROBE)
{
}

bool Sample::CmdViewGRepProbe::IsEnabled() const
{
    if (!GcmpCommandState::IsInDocumentEnvironment())
    {
        return false;
    }
    if (ISelection* pSelection = ISelection::Get())
    {
        std::unordered_set<ElementId, ElementIdHash> elemMaps = pSelection->GetSelectedElements();
        if (elemMaps.size() == 1)
        {
            return true;
        }
    }
    return false;
}

bool Sample::CmdViewGRepProbe::IsVisible() const
{
    return GcmpCommandState::IsInDocumentEnvironment();
}

gcmp::OwnerPtr<gcmp::IAction> Sample::CmdViewGRepProbe::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    IUiDocumentViewManager* pUiDocViewMgr = IUiDocumentViewManager::Get();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiDocViewMgr, L"获取UI文档视图管理器失败", L"GDMP", L"2024-03-30");
    IUiDocument* pUiDoc = pUiDocViewMgr->GetCurrentUiDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiDoc, L"获取UI文档失败", L"GDMP", L"2024-03-30");
    IDocument* pDoc = pUiDoc->GetDbDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"获取文档失败", L"GDMP", L"2024-03-30");

    ISelection* pSelection = ISelection::Get();
    std::unordered_set<ElementId, ElementIdHash> elemMaps = pSelection->GetSelectedElements();
    if (elemMaps.size() == 0)
    {
        UiCommonDialog::ShowMessageBox(L"提示", L"没有选择图元，因此探针无效。", (int)UiCommonDialog::ButtonType::Yes);
        return nullptr;
    }

    ElementId id = *elemMaps.begin();

    GRepProbe* pGRepProbe = GRepProbe::Get();
    pGRepProbe->Execute(true, pDoc, id);

    return nullptr;
}

REGISTER_COMMAND(CmdGRepProbe)

CmdGRepProbe::CmdGRepProbe()
    :CommandBase(ID_CMD_GREP_PROBE)
{
}

bool CmdGRepProbe::IsEnabled() const
{
    if (!GcmpCommandState::IsInDocumentEnvironment())
    {
        return false;
    }
    // 选且只选了一个图元
    if (ISelection* pSelection = ISelection::Get())
    {
        std::unordered_set<ElementId, ElementIdHash> elemMaps = pSelection->GetSelectedElements();
        if (elemMaps.size() == 1)
        {
            return true;
        }
    }
    return false;
}

bool CmdGRepProbe::IsVisible() const
{
    return GcmpCommandState::IsInDocumentEnvironment();
}


gcmp::OwnerPtr<gcmp::IAction> CmdGRepProbe::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    IUiDocumentViewManager* pUiDocViewMgr = IUiDocumentViewManager::Get();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiDocViewMgr, L"获取UI文档视图管理器失败", L"GDMP", L"2024-03-30");
    IUiDocument* pUiDoc = pUiDocViewMgr->GetCurrentUiDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiDoc, L"获取UI文档失败", L"GDMP", L"2024-03-30");
    IDocument* pDoc = pUiDoc->GetDbDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"获取文档失败", L"GDMP", L"2024-03-30");

    ISelection* pSelection = ISelection::Get();
    std::unordered_set<ElementId, ElementIdHash> elemMaps = pSelection->GetSelectedElements();
    if (elemMaps.size() == 0)
    {
        UiCommonDialog::ShowMessageBox(L"提示", L"没有选择图元，因此探针无效。", (int)UiCommonDialog::ButtonType::Yes);
        return nullptr;
    }

    ElementId id = *elemMaps.begin();

    GRepProbe* pGRepProbe = GRepProbe::Get();
    pGRepProbe->Execute(false, pDoc, id);

    return nullptr;
}


